package com.sample.mockito.test;

import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.exceptions.verification.SmartNullPointerException;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import static org.mockito.Mockito.*;

/*
https://www.cnblogs.com/s1328/p/4626757.html
 */
public class SampleTest05 {

    @Test(expected = SmartNullPointerException.class)
    public void sampleTest14() {
        LinkedList mockedList = mock(LinkedList.class, Mockito.RETURNS_SMART_NULLS);

        ListIterator iter = mockedList.listIterator();

        System.out.println(iter.hasNext());
    }

    @Test
    public void sampleTest15() {
        List mock = mock(List.class);
        List mock2 = mock(List.class);
        mock.add("John");
        mock2.add("Brian");
        mock2.add("Jim");

        ArgumentCaptor argument1 = ArgumentCaptor.forClass(String.class);

        verify(mock).add(argument1.capture());
        assertEquals("John", argument1.getValue());

        ArgumentCaptor argument2 = ArgumentCaptor.forClass(String.class);
        verify(mock2, times(2)).add(argument2.capture());

        assertEquals("Jim", argument2.getValue());
        assertArrayEquals(new Object[]{"Brian", "Jim"}, argument2.getAllValues().toArray());
    }

    @Test
    public void sampleTest16() {
        LinkedList mock = mock(LinkedList.class);
        when(mock.get(1)).thenReturn("hello");
        when(mock.add(anyString())).thenCallRealMethod();

        System.out.println(mock.get(1));
        System.out.println(mock.add("0"));
        System.out.println(mock.get(0));
    }

    @Test
    public void sampleTest17() {

        List mock = mock(List.class);
        when(mock.size()).thenReturn(10);
        System.out.println(mock.size());

        reset(mock);
        System.out.println(mock.size());
    }


}